const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

var isSend = 0;

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    mobile: '',
    name:'',
    msgCode: '',
    msgIntervalTime: 60,
    msgIntervalTimeLast: 60,
    sendMsgShow: 1,
    showModel:false
  },
  onLoad: function (options) {
    isSend = 0;
  },
  onShow: function () {
    this.checkLogin();
  },
  checkLogin: function () {
    var memberId = wx.getStorageSync("memberId");
    var isAuthorize = 0;
    if (memberId) {
      isAuthorize = 1;
    }
    this.setData({
      isAuthorize: isAuthorize
    });
  },
  getmobile: function (e) {
    var mobile = e.detail.value;
    this.setData({
      mobile: mobile
    });
  },
  getname: function (e) {
    var name = e.detail.value;
    this.setData({
      name: name
    });
  },
  getmsgCode: function (e) {
    var msgCode = e.detail.value;
    this.setData({
      msgCode: msgCode
    })
  },
  //获取验证码
  sendMsg: function () {
    var isAuthorize = this.data.isAuthorize;
    if (isAuthorize == 0) {
      wx.navigateTo({
        url: '/pages/shouquan/shouquan',
      });
      return false;
    }
    if (isSend == 1) {
      return false;
    }
    isSend = 1;
    var that = this;
    var mobile = this.data.mobile;
    if (mobile == null || mobile == '') {
      common.showErrorToast('请输入手机号');
      isSend = 0;
      return false;
    }
    var data = {
      mobile: mobile,
      type: 1
    }
    common.request(api.yezhusendcode, data, 'POST').then(function (data) {
      if (data.type === 'success') {
        // if(data.content!='您还不是业主！'){
        //    app.globalData.isyezhu=true;
        // }
        common.showSuccessToast(data.content); 
        isSend = 0;
        //屏蔽发送按钮+倒计时 
        that.setData({
          sendMsgShow: 0
        })
        var msgIntervalTime = that.data.msgIntervalTime;
        var msgIntervalTimeLast = that.data.msgIntervalTimeLast;
        msgIntervalTimeLast = msgIntervalTime;
        that.setData({
          msgIntervalTimeLast: msgIntervalTimeLast
        });
        var interval = setInterval(function () {
          that.setData({
            msgIntervalTimeLast: msgIntervalTimeLast
          })
          msgIntervalTimeLast --;
          if (msgIntervalTimeLast == 0) {
            that.setData({
              sendMsgShow: 1
            })
            clearInterval(interval);
          }
        }.bind(this), 1000);
      } else {
        common.showErrorToast(data.content);
        isSend = 0;
      }
    });
  },
  hideModal(e) {
    this.setData({
      showModel: false
    })
    setTimeout(function () {
      wx.navigateBack({
        delta: 1
      })
    }, 500);

  },
  //提交信息
  tijiao: function (e) {
    var isAuthorize = this.data.isAuthorize;
    if (isAuthorize == 0) {
      wx.navigateTo({
        url: '/pages/shouquan/shouquan',
      });
      return false;
    }
    var mobile = this.data.mobile;
    var msgCode = this.data.msgCode;
    var name = this.data.name;
    if (name == null || name == '') {
      common.showErrorToast('请输入姓名');
      return false;
    }
    if (mobile == null || mobile == '') {
      common.showErrorToast('请输入手机号');
      return false;
    }
    if (msgCode == null || msgCode == '') {
      common.showErrorToast('请输入验证码');
      return false;
    }
    var data = {
      mobile:mobile,
      code:msgCode,
      userName:name
    }
    let that = this;
    common.request(api.infoBind, data, 'POST').then(function (data) {
      if (data.type === 'success') {
        // common.showSuccessToast(data.content);
        wx.setStorageSync('isBind', '1')
        that.setData({
          cgmsg:data.content,
          showModel:true
        })
        
      } else {
        common.showErrorToast(data.content);
      }
    });
  }

})